package com.ns002.yigou.quote.web;

import com.ns002.yigou.quote.common.task.LoadRealTimeQuoteTask;
import com.ns002.yigou.quote.domain.quotekline.param.LastNodeParam;
import com.ns002.yigou.quote.domain.quotekline.result.LastNodeResult;
import com.ns002.yigou.quote.entity.KLine;
import com.ns002.yigou.quote.json.JSONResult;
import com.ns002.yigou.quote.service.KLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author xu
 * @Date 2017/8/4 0004 上午 11:08
 * K线行情
 *
 *
 *   @null           验证对象是否为空
     @notnull     验证对象是否为非空
     @asserttrue       验证 boolean 对象是否为 true
     @assertfalse      验证 boolean 对象是否为 false
     @min           验证 number 和 string 对象是否大等于指定的值
     @max           验证 number 和 string 对象是否小等于指定的值
     @decimalmin    验证 number 和 string 对象是否大等于指定的值，小数存在精度
     @decimalmax    验证 number 和 string 对象是否小等于指定的值，小数存在精度
     @size           验证对象（array,collection,map,string）长度是否在给定的范围之内
     @digits       验证 number 和 string 的构成是否合法
     @past           验证 date 和 calendar 对象是否在当前时间之前
     @future       验证 date 和 calendar 对象是否在当前时间之后
     @pattern     验证 string 对象是否符合正则表达式的规则

     @Email     验证邮箱
     *
 */
@RestController
@RequestMapping(value = "/market/kline")
@Api(tags = "行情K线接口")
@SuppressWarnings("unchecked")
public class QuoteKLineController {

    private static Logger log = LoggerFactory.getLogger("kline-controller");
    @Autowired
    KLineService kLineService;

    @ApiOperation(value = "最后的K线节点", notes = "返回当前尚未生成的最后一根K线节点")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 666, message = "失败")
    })
    @GetMapping(value = "/last")
    public JSONResult<LastNodeResult> lastNode(@Valid LastNodeParam lastNodeParam, BindingResult bindingResult){

        try{
            JSONResult.handleBindingResult(bindingResult);

            String type = null;
            switch (lastNodeParam.getType()) {
                case "2":
                    type = "M5";
                    break;
                case "3":
                    type = "M15";
                    break;
                case "4":
                    type = "M30";
                    break;
                case "5":
                    type = "H1";
                    break;
                case "6":
                    type = "H4";
                    break;
            }

            LastNodeResult result = LoadRealTimeQuoteTask.getLastKLine(type, lastNodeParam.getContract());

            if(result == null){
                 return JSONResult.createSuccessResult(null);
            }

            result.setStartDate(result.getEndDate());

            return JSONResult.createSuccessResult(result);
        } catch (Throwable e){
            return JSONResult.createErrorResult(e);
        }


    }

}
